//抽奖方式1
(function(w){
    var lottery = function(){
            this.init();
            this.evt();
            return this;
        },
        c,//当前对象
        currentSite = 0,//当前位置
        runnNumber = 16+7,//要经过的方块数
        x = 0,y = 0,//移动方块走的距离
        speed = 1;//速度控制

    lottery.prototype = {
        init: function(){
            c = this;

        },
        $: function(id){
            return document.getElementById(id);
        },
        run: function(){
            if(currentSite < 2)x += 96;else

            if(currentSite < 4)y += 96;else

            if(currentSite < 6)x -= 96;else

            if(currentSite < 8)y -= 96;

            currentSite++;
            if(currentSite > 7)currentSite = 0;
            c.$("sporter").style.transform = "translate3d("+x+"px,"+y+"px,0)";
            c.$("sporter").style.webkitTransform = "translate3d("+x+"px,"+y+"px,0)";

        },
        evt: function(){
            c.$("start").addEventListener("click",function(){
                if(runnNumber <= 0)return;
                c.run();
            });
            c.$("sporter").addEventListener("webkitTransitionEnd",function(){
                if(runnNumber <= 1){runnNumber = 0; return;}
                runnNumber--;
                speed++;
                if(speed == 4){
                    this.style.transition = "transform 200ms ease-in-out";
                }
                if(runnNumber == 4){
                    this.style.transition = "transform 600ms ease-in-out";
                }
                c.run();
            });
        }

    };
    w.lottery = lottery;

}(window));
//new lottery();
//抽奖方式2
(function(w){
    var lottery2 = function(data){
            this.init();
            this.evt(data);
            return this;
        },
        c,
        currentSite = 0,//当前位置
        runNumber = 32,//要经过的方块数
        slow = 0,
        fast = 0,
        timeCount = 600,
        canClick = true,
        toListType = getQueryString("type"),
        isLucky = "";

    lottery2.prototype = {
        init: function(){
            c = this;
        },
        run: function(data){
            sessionStorage.number = Number($("#start").find("i").text()) - 1;
            $("#start").find("i").text(Number($("#start").find("i").text()) - 1);
            runNumber = 32;
            runNumber += data.data.position;
            runNumber -= currentSite;
            if(runNumber == 0){
                return;
            }
            isLucky = data.data.isLucky;
            timeCount = 600;
            c.toFast();
        },
        toFast: function(){
            slow = setTimeout(function(){
                $("#"+currentSite%8).removeClass("lottery-selected");
                if(currentSite%8 == 7){
                    $("#0").addClass("lottery-selected");
                }else{
                    $("#"+(currentSite%8+1)).addClass("lottery-selected");
                }
                currentSite++;
                runNumber--;
                //timeCount -= 100;

                if(timeCount <= 200){
                    //clearInterval(slow);
                    $("#start").trigger("trigger");
                    return;
                }
                timeCount -= 100;
                c.toFast(timeCount);
            },timeCount);
        },
        toSlow: function(){
            slow = setTimeout(function(){
                $("#"+currentSite%8).removeClass("lottery-selected");
                if(currentSite%8 == 7){
                    $("#0").addClass("lottery-selected");
                }else{
                    $("#"+(currentSite%8+1)).addClass("lottery-selected");
                }

                currentSite++;
                runNumber--;
                //timeCount += 200;
                if(runNumber <= 0){
                    //clearInterval(slow);
                    setTimeout(function(){
                        currentSite = currentSite%8;
                        if(Number($("#start").find("i").text()) <= 0){
                            if(!isLucky){
                                //tip.alert({content:"今天的抽奖机会已经全部用完<br>明天请继续"});
                                tip.roundLotteryTip({title:$("#"+currentSite).find("span").text(),isReward:false});
                                $("#start").addClass("reward-over");
                            } else{
                                tip.roundLotteryTip({title:$("#"+currentSite).find("span").text(),isReward:true});
                                $("#start").addClass("reward-over");
                            }
                            return;
                        }
                        if(!isLucky){
                            tip.roundLotteryTip({title:$("#"+currentSite).find("span").text(),isReward:false});
                        }else{
                            tip.roundLotteryTip({title:$("#"+currentSite).find("span").text(),isReward:true});
                        }
                        //canClick = true;
                        cw.buttonAjax = true;
                    },1000);
                    return;
                }
                timeCount += 60;
                c.toSlow();
            },timeCount);
        },
        errorLog: function(res,e){
            netWork({"url":cw.serverUrl+"/rest/v1.0/Systems/appErrorLog/1",data:"userId="+lotteryObj.uId+"&errorMsg="+e+":"+JSON.stringify(res),method:"post"});
        },
        evt: function(data){
            var nextData = data;
            $("#start")[0].addEventListener("click",function(){
                if(!lotteryObj.vConfirm){
                    if (toListType == 'ios') {
                        document.location.href = "66xiaoqu:Question:vAuth";
                    } else {
                        webviewhandle.goToVcommitSelect();//webviewhandle为webview注册对象
                    }
                    //setTimeout(function(){
                    //    document.location.href = "";
                    //},200);
                    return false;
                }
                if($("#start")[0].className.match("reward-over")){
                    tip.alert({content:"今天的抽奖机会已经全部用完<br>明天请继续"});
                }
                if(sessionStorage.number>=0)lotteryObj.lotteryNum = sessionStorage.number;
                if(cw.buttonAjax&&Number($("#start").find("i").text()) > 0&&lotteryObj.lotteryNum){
                    //canClick = false;
                    cw.buttonAjax = false;
                    nextData.prize = true;
                    nextData.logger = c.errorLog;
                    netWork(nextData,c.run);
                }
            });

            $("#start").on("trigger",function(){
                fast = setInterval(function(){
                    $("#"+currentSite%8).removeClass("lottery-selected");
                    if(currentSite%8 == 7){
                        $("#0").addClass("lottery-selected");
                    }else{
                        $("#"+(currentSite%8+1)).addClass("lottery-selected");
                    }
                    currentSite++;
                    runNumber--;
                    if(runNumber <= 6){
                        clearInterval(fast);
                        $("#start").trigger("trigger2");
                    }
                },150);
            });

            $("#start").on("trigger2",function(){
                c.toSlow();
            });

            $("#start")[0].addEventListener("touchstart",function(){
                if(lotteryObj.lotteryNum == 0 || !cw.buttonAjax)return;
                this.className = "start-button-pressed";
            });
            $("#start")[0].addEventListener("touchend",function(){
                if(lotteryObj.lotteryNum == 0 || !cw.buttonAjax)return;
                this.className = "";
            });

        }
    };
    w.lottery2 = lottery2;

}(window));

//抽奖列表
(function(w){
    var lotteryList = function(data){
        this.init(data);
        return this;
    },direction = true;
    lotteryList.prototype = {
        init: function(data){
            netWork(data,this.run);
            this.evt();
        },
        run: function(data){
            var html = "",date;//<marquee direction='up' scrollamount='6' behaviour='scroll' scrolldelay='0' loop='infinite'>
            //data.data.length = 2;
            data.data.forEach(function(item){
                //date = new Date(item.time);
                html += "<li><span class = 'ell'>"+item.nickName+"</span><span class = 'ell'>获得"+item.prize+"</span><span class = 'ell'>"+formatTimeRule(item.time)+"</span></li>";
            });
            //</marquee>
            $("#lot_list").html(html);

            //var a = 1,b = 0,c = -1,d = -2,list;
            //if(count < 5){
            //    return;
            //}
            //else if(count < 10){
            //
            //}   div1 = document.querySelector("#lottery_list div:nth-child(1)"),
            //    div2 = document.querySelector("#lottery_list div:nth-child(2)"),
            //    //div3 = document.querySelector("#lottery_list div:nth-child(3)"),
            //    //div4 = document.querySelector("#lottery_list div:nth-child(4)"),
            //    list = setInterval(function(){
            //        div1.style.transform = "translate3d(0,"+a*100+"%,0)";
            //        div1.style.webkitTransform = "translate3d(0,"+a*100+"%,0)";
            //
            //        div2.style.transform = "translate3d(0,"+b*100+"%,0)";
            //        div2.style.webkitTransform = "translate3d(0,"+b*100+"%,0)";
            //
            //        //div3.style.transform = "translate3d(0,"+c*100+"%,0)";
            //        //div3.style.webkitTransform = "translate3d(0,"+c*100+"%,0)";
            //        //
            //        //div4.style.transform = "translate3d(0,"+d*100+"%,0)";
            //        //div4.style.webkitTransform = "translate3d(0,"+d*100+"%,0)";
            //
            //        if(a > 1){a = -1;}else{div1.style.display = "block";}a++;
            //        if(b > 1){b = -1;}else{div2.style.display = "block";}b++;
            //        //if(c > 1){c = -2;div3.style.display = "none";}else{div3.style.display = "block";}c++;
            //        //if(d > 1){d = -2;div4.style.display = "none";}else{div4.style.display = "block";}d++;
            //    },2000);
            setTimeout(function(){
                $("#lottery_list ul").trigger("scroll");
            },500);
        },
        evt: function(){
            $(document).on("scroll","#lottery_list ul",function(){
                if($(this)[0].offsetHeight > $("#lottery_list")[0].offsetHeight) {
                    $(this)[0].style.webkitTransition = "-webkit-transform 20s linear";
                    $(this)[0].style.webkitTransform = "translate3d(0,-100%,0)";
                }
            });
            $(document).on("webkitTransitionEnd","#lottery_list ul",function(){
                if(direction){
                    var sLength = 0,obj = $(this)[0];
                    obj.style.opacity = 0;
                    setTimeout(function(){
                        sLength = parseInt(100/($("#lottery_list ul")[0].offsetHeight)*100);
                        //100/$("#lottery_list ul").height()
                        //$(this)[0].style.webkitTransition = "rotateY(90deg)";
                        obj.style.webkitTransition = "-webkit-transform 24ms linear";
                        obj.style.transition = "transform 24ms linear";
                        obj.style.webkitTransform = "translate3d(0,"+sLength+"%,0)";
                        direction = false;
                    },200);
                }else {
                    $(this)[0].style.opacity = 1;
                    //$(this)[0].style.webkitTransition = "rotateY(0deg)";
                    $(this)[0].style.webkitTransition = "-webkit-transform 20s linear";
                    $(this)[0].style.transition = "transform 20s linear";
                    $(this)[0].style.webkitTransform = "translate3d(0,-100%,0)";
                    direction = true;
                }

                //$(this).trigger("scrollend");
            });
            $(document).on("scrollend","#lottery_list ul",function() {
                $("#lottery_list ul").trigger("scroll");
            });
            $(document).on("touchmove",".list-area",function(e) {
                e.stopPropagation();
                //e.preventDefault();
            });
        }
    };
    w.lotteryList = lotteryList;
}(window));

//主文件
(function(w){
    var lotteryIndex = function(){
        this.init();
        this.evt();
        return this;
        }, c,
        userId = getQueryString("userObjId"),
        lotteryId = getQueryString("giftActivityObjId"),
        clickSource = getQueryString("clickSource");

    lotteryIndex.prototype = {
        init: function(){
            c = this;
            this.data = {"url":cw.serverUrl+"/rest/v1.0/lottery/detail/"+userId+"/"+lotteryId+"?clickSource="+clickSource,native: false};
            netWork(this.data,this.initData);

            this.data.url = cw.serverUrl+"/rest/v1.0/lottery/luckyUsers/"+userId+"/"+lotteryId+"/1";
            new lotteryList(this.data);

            this.data.url = cw.serverUrl+"/rest/v1.0/lottery/draw/"+userId+"/"+lotteryId;
            new lottery2(this.data);
        },
        initData: function(data){
            var sdate = new Date(data.data.startTime),edate = new Date(data.data.endTime),now = new Date();
            if(now < sdate){
                $("#mask").css("display","block");
                $("body").append("<div class = 'no-time'><img src='"+cw.resUrl+"../images/no_start.png'><div>活动时间是<br>"+sdate.getFullYear()+"."+numTowDisplay(sdate.getMonth()+1)+"."+numTowDisplay(sdate.getDate())+"-"+edate.getFullYear()+"."+numTowDisplay(edate.getMonth()+1)+"."+numTowDisplay(edate.getDate()));
            }else if(now > edate){
                $("#mask").css("display","block");
                $("body").append("<div class = 'no-time'><img src='"+cw.resUrl+"../images/al_end.png'><div>活动时间是<br>"+sdate.getFullYear()+"."+numTowDisplay(sdate.getMonth()+1)+"."+numTowDisplay(sdate.getDate())+"-"+edate.getFullYear()+"."+numTowDisplay(edate.getMonth()+1)+"."+numTowDisplay(edate.getDate()));
            }
            $("#start_time").text("活动时间："+numTowDisplay(sdate.getMonth()+1)+"月"+numTowDisplay(sdate.getDate())+"日 "+numTowDisplay(sdate.getHours())+":"+numTowDisplay(sdate.getMinutes())+"—"+numTowDisplay(edate.getMonth()+1)+"月"+numTowDisplay(edate.getDate())+"日 "+numTowDisplay(edate.getHours())+":"+numTowDisplay(edate.getMinutes()));

            data.data.prizeList.forEach(function(item){
                console.log(item.position);
                $("#"+item.position).css("background","url("+item.pic+") no-repeat");
                $("#"+item.position).css("background-size","100% 100%");
                $("#"+item.position).find("span").text(item.name);
            });
            if(sessionStorage.number)
                $("#start").find("i").text(sessionStorage.number);
            else
                $("#start").find("i").text(data.data.lotteryNum);

            c.lotteryNum = data.data.lotteryNum;
            if(data.data.lotteryNum == 0){$("#start").addClass("reward-over");}

            $(".start-loading").css("display","none");
            c.vConfirm = data.data.isAuth;
        },
        evt: function(){
            $("#my_reward").on("click",function(){
                location.href = "/pages/roundLottery/html/my_list.html?userId="+userId+"&lotteryId="+lotteryId+"&version="+Math.random();
            });
            $("#reward_list").on("click",function(){
                location.href = "/pages/roundLottery/html/list.html?userId="+userId+"&lotteryId="+lotteryId+"&version="+Math.random();
            });
        },
        lotteryNum: 0,
        vConfirm: true,
        uId: userId
    };

    w.lotteryIndex = lotteryIndex;
}(window));
var lotteryObj = new lotteryIndex();

//tip.roundLotteryTip({title:"6情值",isOver:false});
//tip.alert();